package dao.mysql;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IObject;
import modelInterfaces.ISubject;
import dao.DAOException;
import dao.IObjectDAO;

/**
 *
 * @author salvador
 */
public class ObjectMySqlDAO implements IObjectDAO {

	public static String PROPERTIES_FILE_PATH = "config/MySqlDAO.props";
	
    private String EVENT_TABLE_NAME = "event";
    private String EVENT_SUBJECT_COLUMN_NAME = "subject";
    private String EVENT_OBJECT_COLUMN_NAME = "object";	
	
    public ObjectMySqlDAO()
    {
    	Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(PROPERTIES_FILE_PATH));
			EVENT_TABLE_NAME = prop.getProperty("EVENT_TABLE_NAME") != null ?					prop.getProperty("EVENT_TABLE_NAME") :			this.EVENT_TABLE_NAME;
			EVENT_SUBJECT_COLUMN_NAME = prop.getProperty("EVENT_SUBJECT_COLUMN_NAME") != null ?	prop.getProperty("EVENT_SUBJECT_COLUMN_NAME") :	this.EVENT_SUBJECT_COLUMN_NAME;
	        EVENT_OBJECT_COLUMN_NAME = prop.getProperty("EVENT_OBJECT_COLUMN_NAME") != null ?	prop.getProperty("EVENT_OBJECT_COLUMN_NAME") :	this.EVENT_OBJECT_COLUMN_NAME;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	@Override
	public List<IObject> retrieveAll() throws DAOException {
		
		String query = "SELECT "+ EVENT_OBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" GROUP BY "+ EVENT_OBJECT_COLUMN_NAME +" ORDER BY "+ EVENT_OBJECT_COLUMN_NAME;
		ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<IObject> subjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject object = AbstractModelFactory.getModelFactory().getItem();
                object.setId(rs.getInt(EVENT_OBJECT_COLUMN_NAME));
                subjectList.add(object);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
    }

	@Override
	public List<IObject> retrieveAllExcept(IObject object) throws DAOException {
		
		String query = "SELECT "+ EVENT_OBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" where "+ EVENT_OBJECT_COLUMN_NAME +" != '"+object.getId()+"' GROUP BY "+ EVENT_OBJECT_COLUMN_NAME +" ORDER BY "+ EVENT_OBJECT_COLUMN_NAME;
		ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<IObject> distinctObjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject distinctObject = AbstractModelFactory.getModelFactory().getItem();
                distinctObject.setId(rs.getInt(EVENT_OBJECT_COLUMN_NAME));
                distinctObjectList.add(distinctObject);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return distinctObjectList;
	}

	@Override
	public List<IObject> retrieveAllFromSubject(ISubject subject) throws DAOException {
		String query = "SELECT "+ EVENT_OBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" where "+ EVENT_SUBJECT_COLUMN_NAME +" ='"+ subject.getId() +"'";
		ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<IObject> subjectList = new ArrayList<IObject>();
        try
        {
            while (rs.next())
            {
            	IObject object = AbstractModelFactory.getModelFactory().getItem();
                object.setId(rs.getInt(EVENT_OBJECT_COLUMN_NAME));
                subjectList.add(object);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
	}
	
	@Override
	public void create(IObject object) throws DAOException {
		// TODO [programador] Auto-generated method stub
		
	}

	@Override
	public void delete(IObject object) throws DAOException {
		// TODO [programador] Auto-generated method stub
		
	}

	@Override
	public void update(IObject object) throws DAOException {
		// TODO [programador] Auto-generated method stub
	}
	
}
